package com.example.libcommon.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.libcommon.R;

public class EmptyView extends LinearLayout {

    private  ImageView icon;
    private  TextView title;
    private  Button action;

    public EmptyView(Context context) {
        this(context,null);
    }

    public EmptyView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //根据第三个参数判断 是否附加到根布局, true则使用传入的root(groupVIew)进行addView将这个资源布局加入,如果为false 则给view设置一个父view的layoutParams并返回这个view
        LayoutInflater.from(context).inflate(R.layout.layout_empty, this, true);
        setOrientation(VERTICAL);
        setGravity(Gravity.CENTER);

        icon = findViewById(R.id.empty_icon);
        title = findViewById(R.id.empty_text);
        action = findViewById(R.id.empty_action);
    }

    public void setEmptyIcon(@DrawableRes int iconRes){
        icon.setImageResource(iconRes);
    }

    public void setTile(String text){
        if (TextUtils.isEmpty(text)){
            title.setVisibility(GONE);
        }else {
            title.setText(text);
            title.setVisibility(VISIBLE);
        }
    }
    public void setButton(String text,OnClickListener listener){
        if (TextUtils.isEmpty(text)){
            action.setVisibility(GONE);
        }else {
            action.setText(text);
            action.setVisibility(VISIBLE);
            action.setOnClickListener(listener);
        }
    }
}
